package com.itheima.day01.managerTest.controller;

import com.itheima.day01.managerTest.domain.Teacher;
import com.itheima.day01.managerTest.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherController {
    TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        studentloop:
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("--------欢迎来到 <教师> 信息管理系统--------");
            System.out.println("请输入您的选择: 1.招聘老师  2.解雇老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    System.out.println("解雇老师");
                    deleteTeacherById();
                    break;
                case "3":
                    System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("欢迎使用教师管理系统！\n");
                    break studentloop;
                default:
                    System.out.println("输入有误，请重新选择：\n");
                    break;
            }
        }
    }

    // 招聘老师
    public void addTeacher() {
        String id;
        loopOut:
        while(true){
            loopIn:
            while (true){
                System.out.println("请录入老师工号:");
                id = sc.next();
                int index = teacherService.seekTeacherId(id);
                if(index == -1){
                    break;
                }else {
                    System.out.println("该工号已被占用，请重新录入");
                    System.out.println("如想退出请按“0”，继续请按任意数字键.");
                }
                String judge = sc.next();
                if("0".equals(judge)){
                    return;
                }
            }

            Teacher addTea = setTeacher(id);
            teacherService.addTeacher(addTea);
            System.out.println("添加成功\n");
            System.out.println("如想退出请按“0”，继续请按任意数字键。");
            String judge = sc.next();
            System.out.println();
            if("0".equals(judge)) {
                return;
            }
        }
    }

    // 解雇老师
    public void deleteTeacherById() {
        boolean boo = teacherService.judgeListTea();
        if (boo == false) {
            System.out.println("系统中暂无数据，请添加后操作。\n");
            return;
        }
        String id;
        while (true){
            System.out.println("请录入要解雇老师工号:");
            id = sc.next();
            int index = teacherService.seekTeacherId(id);
            if(index != -1){
                teacherService.deleteTeacherById(index);
                System.out.println("删除成功。");
                System.out.println("如想退出请按“0”，继续请按任意数字键。");
            }else {
                System.out.println("您输入的老师工号没有对应的老师，请查证后操作。");
                System.out.println("如想退出请按“0”，继续请按任意数字键。");
            }
            String judge = sc.next();
            System.out.println();
            if("0".equals(judge)){
                return;
            }
        }


    }

    // 修改老师
    public void updateTeacher() {
        boolean boo = teacherService.judgeListTea();
        if (boo == false) {
            System.out.println("系统中暂无数据，请添加后操作。\n");
            return;
        }
        String id;
        while (true){
            System.out.println("请录入要修改老师工号:");
            id = sc.next();
            int index = teacherService.seekTeacherId(id);
            if(index != -1){
                teacherService.updateTeacher(index, id);
                System.out.println("修改成功。\n");
                System.out.println("如想退出请按“0”，继续请按任意数字键。");
            }else {
                System.out.println("您输入的老师工号没有对应的老师，请查证后操作。");
                System.out.println("如想退出请按“0”，继续请按任意数字键。");
            }
            String judge = sc.next();
            System.out.println();
            if("0".equals(judge)){
                return;
            }
        }


    }

    // 查看老师
    public void findAllTeacher() {
        boolean boo = teacherService.judgeListTea();
        if (boo == false) {
            System.out.println("系统中暂无数据，请添加后查看。\n");
            return;
        }

        ArrayList<Teacher> listTea = teacherService.findAllTeacher();
        System.out.println("工号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < listTea.size(); i++) {
            Teacher tea = listTea.get(i);
            System.out.println(tea.getId() + "\t" +
                                tea.getName() + "\t" +
                                tea.getAge() + "\t\t" +
                                tea.getBirthday());
        }
        System.out.println();
    }

    // 键盘录入老师信息，返回老师对象
    public Teacher setTeacher(String id){

        System.out.println("请录入老师姓名:");
        String name = sc.next();
        System.out.println("请录入老师年龄:");
        int age = sc.nextInt();
        System.out.println("请录入老师生日:");
        String birthday = sc.next();

        Teacher teacher = new Teacher();

        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
